package com.mobile;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.net.Uri;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.util.Log;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class MobileInfo
{
    Activity targetAct;
    public MobileInfo()
    {

    }

    public  void Init(Activity currentActivity)
    {
        targetAct = currentActivity;
    }

    //是否有光传感器
    public boolean hasLightSensorManager()
    {
        try{
            SensorManager sm = (SensorManager)targetAct.getSystemService(Context.SENSOR_SERVICE);
            Sensor lightSensor = sm.getDefaultSensor(Sensor.TYPE_LIGHT);
            if(null == lightSensor)
            {
                return false;
            }
            else {
                return true;
            }
        }
        catch(Exception ex)
        {

        }
        return false;
    }

    //读取cpu信息
    public String readCpuInfo()
    {
        String result = "";
        String[] cpuInfo = { "", "" };
        try{
            String[] args = {"/system/bin/cat","/proc/cpuinfo"};
            ProcessBuilder cmd = new ProcessBuilder(args);
            Process process = cmd.start();
            StringBuffer sb = new StringBuffer();
            String readLine = "";
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            while ((readLine = reader.readLine()) != null) {
                //sb.append(readLine);

                if (readLine.toLowerCase().indexOf("hardware") != -1) {
                    cpuInfo[0] = readLine;
                    break;
                }
            }
            cpuInfo[1] = Build.HARDWARE;
            reader.close();
            result = cpuInfo[0] + "&" + cpuInfo[1];
        }
        catch(Exception ex)
        {

        }

        return result;
    }

    //判断cpu是否是真机cpu
    private boolean checkIfIsPhoneCpu()
    {
        String cpuInfo = readCpuInfo();
        if(cpuInfo.contains("intel") || cpuInfo.contains("amd"))
        {
            return false;
        }
        return true;
    }

    public boolean isEmulator() {
        String url = "tel:" + "123456";
        Intent intent = new Intent();
        intent.setData(Uri.parse(url));
        intent.setAction(Intent.ACTION_DIAL);
        // 是否可以处理跳转到拨号的 Intent
        boolean canResolveIntent = intent.resolveActivity(targetAct.getPackageManager()) != null;

        return Build.FINGERPRINT.startsWith("generic")
                || Build.FINGERPRINT.toLowerCase().contains("vbox")
                || Build.FINGERPRINT.toLowerCase().contains("test-keys")
                || Build.MODEL.contains("google_sdk")
                || Build.MODEL.contains("Emulator")
                || Build.SERIAL.equalsIgnoreCase("unknown")
                || Build.SERIAL.equalsIgnoreCase("android")
                || Build.MODEL.contains("Android SDK built for x86")
                || Build.MANUFACTURER.contains("Genymotion")
                || (Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic"))
                || "google_sdk".equals(Build.PRODUCT)
                || ((TelephonyManager) targetAct.getSystemService(Context.TELEPHONY_SERVICE))
                .getNetworkOperatorName().toLowerCase().equals("android")
                || !canResolveIntent
                || !hasLightSensorManager()
                || !checkIfIsPhoneCpu();
    }
}
